package com.amat0urheu.dao;

import com.amat0urheu.pojo.CloudPojo.VMservice;
import com.amat0urheu.pojo.CloudPojo.VMservice;
import com.amat0urheu.pojo.Experiment;
import com.amat0urheu.pojo.ExperimentScores;
import com.amat0urheu.pojo.Notice;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.pojo.race.FlagLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface ExperimentsMapper extends BaseMapper<Experiment> {



    @Select("select * from mc_experiment order by eid asc")
    List<Experiment> selectExperimentByPage();

    @Select("select * from mc_experiment where eid=#{id}")
    Experiment getExperimentByIdForUser(String id);

    @Update("update mc_experiment set ename=#{experiment.ename},edescription=#{experiment.edescription},etext=#{experiment.etext} ,deadTime =#{experiment.deadTime} where eid=#{experiment.eid}")
    int updateExperiment(@Param("experiment") Experiment experiment);
    @Insert("INSERT INTO mc_vm (vmID,proxyNum, experiment,vmName,type,noVNC,createTime,deadTime,status,openstackIP) VALUES (#{experimentvm.vmID},#{experimentvm.proxyNum},#{experimentvm.experiment},#{experimentvm.vmName},#{experimentvm.type},#{experimentvm.noVNC},#{experimentvm.createTime},#{experimentvm.deadTime},#{experimentvm.status},#{experimentvm.openstackIP})")
    int insertEmxperimentVm(@Param("experimentvm") VMservice experimentVm);
    @Select("select * from mc_vm  where race is null order BY createTime DESC")
    List<VMservice> selectExperimentVMByPage();

    @Select("select count(*) from mc_vm where   experiment=#{experiment} and user is null and status='ACTIVE'   and deadTime > CURRENT_TIMESTAMP")
    int selectCountNoUser(String experiment);
    @Update("update mc_vm set user=#{name} where proxyNum=#{proxyNum}  ")
    void setVmServerForUser(String name,Integer proxyNum);
    @Select("select proxyNum from mc_vm where  experiment=#{experimentName} and user is null  AND status='ACTIVE'  order by proxyNum limit 1")
    int findProxyNumNoUser(String experimentName);
    @Select("select vmID,experiment,vmName,type,noVNC,deadTime from mc_vm where user=#{name} and experiment=#{experimentName} and deadTime > CURRENT_TIMESTAMP")
    List<VMservice> getExperimentVmsforUser(String name,String experimentName);

    @Select("select ename from mc_experiment  where eid=#{id}")
    String getExperimentName(Integer id);
    @Select("select eid from mc_experiment  where ename=#{ename}")
    Integer getExperimentId(String  ename);

    @Select("select count(*) from mc_vm where    user = #{name} ")
    int judgeIfHaveVMOfExperiment(String experimentName, String name);

    @Select("select ename from mc_experiment ")
    List<String> selectExperimentNames();

    @Select("select ename,eid from mc_experiment ")
    List<Experiment> getAllExperiments();
    @Select("select eid,ename from mc_experiment order by eid asc")
    List<Experiment> getExperimentNamesAndId();
    @Select("SELECT a.uid,a.uname,b.routineScore,b.reportScore FROM mc_user a LEFT JOIN mc_score b ON a.uid = b.uId ")
    List<ExperimentScores> getAllUserScores();


    @Select("select score from   mc_experiment_score WHERE eId= #{eId} and uId = #{uId}")
    Integer getScoreOfExperiment(Integer eId,Integer uId);
    @Update({
            "INSERT INTO mc_score (uID,routineScore , reportScore) VALUES (#{uid}, #{routineScore}, #{reportScore})",
            "ON DUPLICATE KEY UPDATE routineScore = #{routineScore} ,reportScore =#{reportScore}"
    })
    int updateSconre(Integer uid, Integer routineScore, Integer reportScore);
    @Insert("INSERT INTO mc_experiment_score (uId, eId, score) VALUES (#{uid}, #{eId}, #{score})")
    void insertExperimentScore(Integer uid, Integer eId, Integer score);
    @Select("select count(*) from   mc_experiment_score WHERE eId= #{eId} and uId = #{uId}")
    int judegeExit(Integer uId, Integer eId);
    @Update("update  mc_experiment_score  set score=#{score} where uId=#{uId} and eId=#{eId}")
    Integer updateExperimentScore(Integer uId, Integer eId, Integer score);
    @Select("select * from mc_experiment  where  ${key} like #{value} ")
    List<Experiment> searchExperiment(String key, String value);
    @Select("SELECT a.uid,a.uname,b.routineScore,b.reportScore FROM mc_user a LEFT JOIN mc_score b ON a.uid = b.uId  where  ${key} like #{value}")
    List<ExperimentScores> getUserScores(String key, String value);
@Insert("insert into  mc_experiment (ename,edescription,etext,createTime,deadTime) values (#{experiment.ename},#{experiment.edescription},#{experiment.etext},#{experiment.createTime},#{experiment.deadTime})")
    boolean saveExperiment(@Param("experiment")  Experiment experiment);
    @Select("select count(*) from mc_experiment where ename=#{ename}")
    int getCountByEname(String ename);
    @Select("select deadTime from mc_experiment where ename=#{experimentName}")
    Date getDeadTimeOfExperiment(String experimentName);
    @Select("select  vmID,openstackIP from mc_vm where experiment=#{experimentName}   and user =#{username}")
    List<VMservice>  getExperiemntVmOfUser(String experimentName, String username);

    @Delete("delete from mc_vm where vmID=#{vmID}")
    int removeVmById(String vmID);
    @Select("select  releasetime  from mc_experiment_release where user=#{username}")
    Date getReleaseTime(String username);
    @Insert("insert into mc_experiment_release (user,releasetime) values(#{username},#{date})")
    void insertReleaseLog(String username, Date date);
    @Insert("update   mc_experiment_release  set releasetime= #{date} where user= #{username}")
    void updateReleaseLog(String username, Date date);
    @Delete("delete from  mc_experiment where eid =#{eid}")
    int removeExperimentByid(Integer eid);
    @Select("select vmID,openstackIP from mc_vm where experiment  is not null ")
    List<VMservice> findAllExperimentVms();

}
